.TH std::ctype 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ctype \- std::ctype

.SH Synopsis
   Defined in header <locale>
   template< class CharT >
   class ctype;

   Class ctype encapsulates character classification features. All stream input
   operations performed through std::basic_istream<CharT> use the std::ctype<CharT> of
   the locale imbued in the stream to identify whitespace characters for input
   tokenization. Stream output operations apply std::ctype<CharT>::widen() to
   narrow-character arguments prior to output.

   std-ctype-inheritance.svg

                                   Inheritance diagram

.SH Specializations

   The standard library is guaranteed to provide the following specializations (they
   are required to be implemented by any locale object):

   Defined in header <locale>
                       provides narrow character equivalents of the minimal "C" locale
   std::ctype<char>    classification. This specialization uses table lookup for
                       character classification
   std::ctype<wchar_t> provides wide character classification appropriate to the native
                       character set

.SH Member types

   Member type Definition
   char_type   CharT

.SH Member functions

   constructor   constructs a new ctype facet
                 \fI(public member function)\fP
   destructor    destructs a ctype facet
                 \fI(protected member function)\fP
   is            invokes do_is
                 \fI(public member function)\fP
   scan_is       invokes do_scan_is
                 \fI(public member function)\fP
   scan_not      invokes do_scan_not
                 \fI(public member function)\fP
   toupper       invokes do_toupper
                 \fI(public member function)\fP
   tolower       invokes do_tolower
                 \fI(public member function)\fP
   widen         invokes do_widen
                 \fI(public member function)\fP
   narrow        invokes do_narrow
                 \fI(public member function)\fP

.SH Member objects

   static std::locale::id id id of the locale
                             \fI(public member object)\fP

.SH Protected member functions

   do_is       classifies a character or a character sequence
   \fB[virtual]\fP   \fI(virtual protected member function)\fP
   do_scan_is  locates the first character in a sequence that conforms to given
   \fB[virtual]\fP   classification
               \fI(virtual protected member function)\fP
   do_scan_not locates the first character in a sequence that fails given
   \fB[virtual]\fP   classification
               \fI(virtual protected member function)\fP
   do_toupper  converts a character or characters to uppercase
   \fB[virtual]\fP   \fI(virtual protected member function)\fP
   do_tolower  converts a character or characters to lowercase
   \fB[virtual]\fP   \fI(virtual protected member function)\fP
   do_widen    converts a character or characters from char to CharT
   \fB[virtual]\fP   \fI(virtual protected member function)\fP
   do_narrow   converts a character or characters from CharT to char
   \fB[virtual]\fP   \fI(virtual protected member function)\fP

Inherited from std::ctype_base

.SH Member types

   Type Definition
   mask unspecified bitmask type (enumeration, integer type, or bitset)

.SH Member constants

   space            the value of mask identifying whitespace character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   print            the value of mask identifying printable character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   cntrl            the value of mask identifying control character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   upper            the value of mask identifying uppercase character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   lower            the value of mask identifying lowercase character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   alpha            the value of mask identifying alphabetic character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   digit            the value of mask identifying digit character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   punct            the value of mask identifying punctuation character classification
   \fB[static]\fP         \fI(public static member constant)\fP
   xdigit           the value of mask identifying hexadecimal digit character
   \fB[static]\fP         classification
                    \fI(public static member constant)\fP
   blank            the value of mask identifying blank character classification
   \fB[static]\fP \fI(C++11)\fP \fI(public static member constant)\fP
   alnum            alpha | digit
   \fB[static]\fP         \fI(public static member constant)\fP
   graph            alnum | punct
   \fB[static]\fP         \fI(public static member constant)\fP

.SH Example

   The following example demonstrates modification of a ctype other than ctype<char> to
   tokenize a CSV file:


// Run this code

 #include <iostream>
 #include <locale>
 #include <sstream>

 struct csv_whitespace : std::ctype<wchar_t>
 {
     bool do_is(mask m, char_type c) const
     {
         if ((m & space) && c == L' ')
             return false; // space will NOT be classified as whitespace

         if ((m & space) && c == L',')
             return true; // comma will be classified as whitespace

         return ctype::do_is(m, c); // leave the rest to the base class
     }
 };

 int main()
 {
     std::wstring in = L"Column 1,Column 2,Column 3\\n123,456,789";
     std::wstring token;

     std::wcout << "default locale:\\n";
     std::wistringstream s1(in);
     while (s1 >> token)
         std::wcout << "  " << token << '\\n';

     std::wcout << "locale with modified ctype:\\n";
     std::wistringstream s2(in);
     csv_whitespace* my_ws = new csv_whitespace;
     s2.imbue(std::locale(s2.getloc(), my_ws));
     while (s2 >> token)
         std::wcout << "  " << token << '\\n';
 }

.SH Output:

 default locale:
   Column
   1,Column
   2,Column
   3
   123,456,789
 locale with modified ctype:
   Column 1
   Column 2
   Column 3
   123
   456
   789

.SH See also

   ctype<char>  specialization of std::ctype for type char
                \fI(class template specialization)\fP
   ctype_base   defines character classification categories
                \fI(class)\fP
   ctype_byname represents the system-supplied std::ctype for the named locale
                \fI(class template)\fP
